package org.xshop.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.xshop.model.TMemberCollection;
import org.xshop.service.ITMemberCollectionService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import top.ibase4j.core.base.AppBaseController;
import top.ibase4j.core.util.DataUtil;
import top.ibase4j.core.util.WebUtil;

/**
 * <p>
 * 收藏 前端控制器
 * </p>
 *
 * @author ZhuLin
 * @since 2017-6-15
 */
@Controller
@RequestMapping("/app/member/")
@Api(tags = "MemberCollection", value = "个人中心-我的收藏接口", description = "APP-个人中心-我的收藏接口")
public class TMemberCollectionController extends AppBaseController<TMemberCollection, ITMemberCollectionService> {
	@ApiOperation(value = "我的收藏列表", notes = "获取当前用户的收藏列表", response = TMemberCollection.class, produces = MediaType.APPLICATION_JSON_VALUE)
	@RequestMapping(value = "getMyCollectionList.api", method = { RequestMethod.GET, RequestMethod.POST })
	public Object query(HttpServletRequest request, TMemberCollection collection) {
		Map<String, Object> param = WebUtil.getParameter(request);
		Long id = getCurrUser(request);
		if (DataUtil.isNotEmpty(id)) {
			param.put("memberId", id);
		}
		Assert.notNull(param.get("memberId"), "用户ID不能为空");
		Object result = service.queryAll(param);
		return setSuccessModelMap(result);
	}

	@Override
	@ApiOperation(value = "添加我的收藏", response = TMemberCollection.class, notes = "添加一条收藏", produces = MediaType.APPLICATION_JSON_VALUE)
	@PostMapping("addCollection.api")
	public Object update(HttpServletRequest request, TMemberCollection param) {
		param = WebUtil.getParameter(request, TMemberCollection.class);
		Long id = getCurrUser(request);
		if (DataUtil.isNotEmpty(id)) {
			param.setMemberId(id);
		}
		Assert.notNull(param.getMemberId(), "用户ID不能为空");
		Assert.notNull(param.getCollectionType(), "收藏类型不能为空");
		Assert.notNull(param.getCollectionId(), "收藏编号不能为空");
		TMemberCollection myCollection = service.selectOne(param);
		ModelMap result =new ModelMap();
		if (myCollection != null) {
			super.delete(myCollection);
			result.put("collect", 0);
		}else {
			super.update(request, new ModelMap(), param);
			result.put("collect", 1);
		}
		return setSuccessModelMap(result);
	}

	@ApiOperation(value = "删除我的收藏", notes = "删除指定ID的收藏", produces = MediaType.APPLICATION_JSON_VALUE)
	@PostMapping("delMemberCollection.api")
	public Object delete(HttpServletRequest request, TMemberCollection param) {
		param = WebUtil.getParameter(request, TMemberCollection.class);
		Long id = getCurrUser(request);
		if (DataUtil.isNotEmpty(id)) {
			param.setMemberId(id);
		}
		Assert.notNull(param.getMemberId(), "用户ID不能为空");
		service.delAll(param);
		return setSuccessModelMap();
	}
}
